package cn.edu.zbu.servlet;

import cn.edu.zbu.cn.edu.zbu.dao.CarDao;
import cn.edu.zbu.cn.edu.zbu.dao.CarDaoImpl;
import cn.edu.zbu.pojo.Car;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AddServlet", value = "/AddServlet")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String name=request.getParameter("name");
        String color=request.getParameter("color");
        String bir=request.getParameter("birthday");//需要转换为日期类型
        String speed=request.getParameter("speed");
        String price=request.getParameter("price");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date birthday=null;
        try {
            birthday=sdf.parse(bir);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Car car=new Car(0,name,color,speed,birthday,price);
        CarDao dao=new CarDaoImpl();
        dao.insert(car);
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request,response);
    }
}
